package com.contactmanager;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class ViewContacts extends Activity {
	
	Contact contact = new Contact();
	
	long id = 0;
	

	private void putContacts(){
		
		ContactsDataSource cds2 = new ContactsDataSource(ViewContacts.this);
		
		cds2.open();
		
    	EditText editText1 = (EditText)findViewById(R.id.editName);
    	EditText editText2 = (EditText)findViewById(R.id.editAddress);
    	EditText editText3 = (EditText)findViewById(R.id.editCellNum);
    	EditText editText4 = (EditText)findViewById(R.id.editAlternate);
    	EditText editText5 = (EditText)findViewById(R.id.editEmail);
    	EditText editText6 = (EditText)findViewById(R.id.editComments);
    	Spinner groupSpinner = (Spinner)findViewById(R.id.groupSpinner);
    	
    	int groupid = groupSpinner.getSelectedItemPosition();
    	
    	contact.setName(editText1.getText().toString());
    	contact.setAddress(editText2.getText().toString());
    	contact.setCell(editText3.getText().toString());
    	contact.setAlt(editText4.getText().toString());
    	contact.setEmail(editText5.getText().toString());
    	contact.setComments(editText6.getText().toString());
    	contact.setGroup(groupid);
    	
    	try {
			cds2.updateContact(contact);
		} catch (Exception e) {
			System.out.println("UPDATE DID NOT WORK! 0_o");
			e.printStackTrace();
		}
		updateTime();
		cds2.close();
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// Restore preferences
		SharedPreferences settings = getSharedPreferences("preferences", 0);
		int theme = settings.getInt("curTheme", R.style.AppBaseTheme);
		System.out.println(theme);
		setTheme(theme);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_view_contacts);
		Intent i = getIntent();
		Contact contactWithId = (Contact)i.getSerializableExtra("Contact");
		ContactsDataSource cds = new ContactsDataSource(this);
		cds.open();
		contact = cds.getContactById(contactWithId.getId());
		cds.close();
		
		
		Spinner spinner = (Spinner)findViewById(R.id.groupSpinner);
		spinner.setSelection(contact.getGroup()-1,true);
		
    	EditText editText1 = (EditText)findViewById(R.id.editName);
    	EditText editText2 = (EditText)findViewById(R.id.editAddress);
    	EditText editText3 = (EditText)findViewById(R.id.editCellNum);
    	EditText editText4 = (EditText)findViewById(R.id.editAlternate);
    	EditText editText5 = (EditText)findViewById(R.id.editEmail);
    	EditText editText6 = (EditText)findViewById(R.id.editComments);
    	editText1.setText(contact.getName());
    	editText2.setText(contact.getAddress());
    	editText3.setText(contact.getCell());
    	editText4.setText(contact.getAlt());
    	editText5.setText(contact.getEmail());
    	editText6.setText(contact.getComments());
    	id = contact.getId();
    	
    	
		//new GetContactTask().execute();
		new GetAllGroupsTask().execute();
		
		Button saveButton = (Button) findViewById(R.id.saveBtn);
		saveButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v){
				ViewContacts.this.putContacts();
				Intent myIntent2 = new Intent(ViewContacts.this, AllContacts.class);
				startActivityForResult(myIntent2, 1);
			}
		});
		
		//Delete button
		Button deleteBtn = (Button) findViewById(R.id.deleteBtn);
		deleteBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v){
				new DeleteContactTask().execute();
				Intent myIntent2 = new Intent(ViewContacts.this, AllContacts.class);
				startActivityForResult(myIntent2, 0);
			}
		});
		
	}

	private class GetAllGroupsTask extends AsyncTask<Void, Integer, ArrayList<Group>>{

		@Override
		protected ArrayList<Group> doInBackground(Void... nothing) {
			
			ContactsDataSource cds3 = new ContactsDataSource(ViewContacts.this);
			
			cds3.open();
			
			ArrayList<Group> groups = cds3.getAllGroups();
			
			cds3.close();
			
			return groups;
		}
		
        @Override
        protected void onPostExecute(ArrayList<Group> groupList) {
    		Spinner spinner = (Spinner) findViewById(R.id.groupSpinner);
    		ArrayAdapter<Group> adapter =  new ArrayAdapter<Group>(ViewContacts.this, android.R.layout.simple_spinner_dropdown_item, groupList);
    		spinner.setAdapter(adapter);      
    		spinner.setSelection(contact.getGroup());
        }
		
	}
	
	//TODO
	private class DeleteContactTask extends AsyncTask<Void, Integer, Void>{

		@Override
		protected Void doInBackground(Void... nothing) {
			
			ContactsDataSource cds2 = new ContactsDataSource(ViewContacts.this);
			
			cds2.open();
			
        	try {
				cds2.deleteContact(contact);
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			cds2.close();
			updateTime();
			return null;
		}
}
	public void updateTime() {
		SharedPreferences settings = getSharedPreferences("preferences", 0);
		SharedPreferences.Editor editor = settings.edit();
		Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long sysTime = -1;
		String myDate = new String(sdf.format(cal.getTime()));
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date;
		try {
			date = format.parse(myDate);
			sysTime=date.getTime();
		} catch (java.text.ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		editor.putLong("lastUpdate",sysTime);
		// Commit the edits!
		editor.commit();
	}

}